package io.gitee.hechenl.datamultitrans.config;

import io.gitee.hechenl.datamultitrans.constants.MqttTopic;
import io.gitee.hechenl.datamultitrans.mqtt.MqttClientInitiator;
import io.gitee.hechenl.datamultitrans.mqtt.MyBroker;
import io.gitee.hechenl.datamultitrans.mqtt.InitClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqttConfig {

    @Bean
    public MyBroker broker1(
            @Value("${mqtt.broker1.name}") String name,
            @Value("${mqtt.broker1.address}") String address,
            @Value("${mqtt.broker1.username}") String username,
            @Value("${mqtt.broker1.password}") String password) {
        return new MyBroker(name, address, username, password);
    }

    @Bean
    public MyBroker broker2(
            @Value("${mqtt.broker2.name}") String name,
            @Value("${mqtt.broker2.address}") String address,
            @Value("${mqtt.broker2.username}") String username,
            @Value("${mqtt.broker2.password}") String password) {
        return new MyBroker(name, address, username, password);
    }

    @Bean
    public MqttClientInitiator mqttClientInitiator(MyBroker broker1) throws Exception {
        MqttClientInitiator initiator = new MqttClientInitiator();
        initiator.init(
                new InitClient("tempClientId", true, broker1, MqttTopic.TEMPERATURE, null, null)
        );
        return initiator;
    }

}